package com.steadyoung.modelapp.base;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.steadyoung.modelapp.R;
import com.trello.rxlifecycle.components.support.RxFragment;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/9/4.
 * Email : steadyoung@foxmail.com
 * Auth  : wayne
 * Desc  : Fragment基类
 */

public abstract class BaseFragment extends RxFragment implements View.OnClickListener {

    /**设置布局ID*/
    protected abstract int getLayoutResId();
    /**初始化数据*/
    protected abstract void initData();
    /**初始化事件监控*/
    protected abstract void initEvent();

    protected boolean isDestory = false;

    protected View mView;

    protected Context mContext;

    protected Activity mActivity;

    Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(getLayoutResId(), null);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mView = getView();
        mContext = getContext();
        mActivity = getActivity();
        unbinder = ButterKnife.bind(this,mView);
        initData();
        initEvent();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    @RequiresApi(api = Build.VERSION_CODES.ECLAIR)
    protected void onLeftClick(){
        mActivity.onBackPressed();
    }

    protected void onRightClick(){
    }

    protected void onCenterClick(){
    }

    /**
     * 设置左侧图片
     * @param res
     */
    protected void setLeftIcon(int res){
        if(mView.findViewById(R.id.title_left_tv) !=null ){
            setDrawableLeft((TextView) mView.findViewById(R.id.title_left_tv),res);
        }
    }

    /**
     * 设置右侧图片
     * @param res
     */
    protected void setRightIcon(int res){
        if(mView.findViewById(R.id.title_right_tv) !=null ){
            setDrawableRight((TextView) mView.findViewById(R.id.title_right_tv),res);
        }
    }

    /**
     * 设置中间图片
     * @param res
     */
    protected void setCenterIcon(int res){
        if(mView.findViewById(R.id.title_center_tv) !=null ){
            setDrawableLeft((TextView) mView.findViewById(R.id.title_center_tv),res);
        }
    }

    /**
     * 设置左侧图片
     * @param res
     */
    protected void setLeftText(int res){
        setText(R.id.title_left_tv,res);
    }

    /**
     * 设置右侧图片
     * @param res
     */
    protected void setRightText(int res){
        setText(R.id.title_right_tv,res);
    }

    /**
     * 设置中间图片
     * @param res
     */
    protected void setCenterText(int res){
        setText(R.id.title_center_tv,res);
    }

    /**
     * 设置textview 的drawable属性(左侧图片)
     *
     * @param tv
     * @param drawableRes 左侧图片
     */
    private void setDrawableLeft(TextView tv, int drawableRes) {
        Drawable drawable = getResources().getDrawable(drawableRes);
        tv.setVisibility(View.VISIBLE);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        tv.setCompoundDrawables(drawable, null, null, null);
        tv.setOnClickListener(this);
    }

    /**
     * 设置textview 的drawable属性（右侧图片）
     *
     * @param tv
     * @param drawableRes 右侧图片
     */
    private void setDrawableRight(TextView tv, int drawableRes) {
        Drawable drawable = getResources().getDrawable(drawableRes);
        tv.setVisibility(View.VISIBLE);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        tv.setCompoundDrawables(null, null, drawable, null);
        tv.setOnClickListener(this);
    }

    private void setText(int viewRes,int textRes){
        if(mView.findViewById(viewRes) !=null ){
            TextView tv = (TextView) mView.findViewById(viewRes);
            tv.setVisibility(View.VISIBLE);
            tv .setText(textRes);
            tv.setOnClickListener(this);
        }
    }

    @SuppressLint("NewApi")
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_center_tv:
                onCenterClick();
                break;
            case R.id.title_left_tv:
                onLeftClick();
                break;
            case R.id.title_right_tv:
                onRightClick();
                break;
            default:
                break;

        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
